package com.duke.shaking.profile;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

import com.duke.shaking.BaseActivity;
import com.duke.shaking.Constants;
import com.duke.shaking.R;
import com.duke.shaking.utils.PhotoUtils;
import com.duke.shaking.widget.ImageCropView;

public class ImageCropActivity extends BaseActivity {
	
	private ImageCropView imageCrop;
	private float cropWidth = Constants.LargeAvatarWidth;
	private float cropHeight = Constants.LargeAvatarHeight;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_image_crop);
        Intent i = getIntent();
        imageCrop = (ImageCropView)findViewById(R.id.imageCrop);
        
        cropWidth = i.getFloatExtra("cropWidth", cropWidth);
        cropHeight = i.getFloatExtra("cropHeight", cropHeight);
        
        String networkUrl = i.getStringExtra("networkUrl");
        if (networkUrl != null) {
        	imageCrop.setBitmap(networkUrl);
        }
        else {
        	boolean fromGallery = i.getBooleanExtra("fromGallery", false);
        	if (fromGallery) {
        		gotoGallery();
        	}
        	else {
        		gotoTakePhoto();
        	}
        }
	}
	
	public void gotoGallery() {
		Intent i = getGalleryPickIntent();
		startActivityForResult(i, Constants.INTENT_CODE_FROM_GALLERY);
	}
	
	public void gotoTakePhoto() {
		Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        //i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
		startActivityForResult(i, Constants.INTENT_CODE_TAKE_PHOTO);
	}
	
	private Intent getGalleryPickIntent() {  
        Intent intent = new Intent(Intent.ACTION_PICK, null);  
        intent.setType("image/*");
        //intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 80);
        intent.putExtra("outputY", 80);
        intent.putExtra("return-data", true);
        return intent;  
    }
	
	protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
		Log.i(TAG, "onActivityResult: resultCode:" + resultCode + ", requestCode:" + requestCode);
		
		if (resultCode != Activity.RESULT_OK) {
			Intent i = new Intent();  
	        setResult(Activity.RESULT_CANCELED, i); 
			finish();
			return;
		}
		
		switch(requestCode) {
		case Constants.INTENT_CODE_TAKE_PHOTO:
			onTakePhotoResult(resultCode, intent);
		case Constants.INTENT_CODE_FROM_GALLERY:
			onFromGalleryResult(resultCode, intent);
		}
	}
	
	public void onTakePhotoResult(int resultCode, Intent intent) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		
		Uri uri = intent.getData();
		if (uri == null) {
			error("get image from camera failed!");
			return;
		}
		imageCrop.setBitmap(uri);
	}
	
	public void onFromGalleryResult(int resultCode, Intent intent) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		Uri uri = intent.getData();
		if (uri == null) {
			error("get image from gallery failed!");
			return;
		}
		
		info("get image from gallery: " + uri.toString());
		imageCrop.setBitmap(uri);
	}
	
	private Bitmap cropImage(Bitmap src, Rect cropRect) {

		Matrix matrix = new Matrix();
		matrix.setScale(cropWidth / cropRect.width(), 
				cropHeight / cropRect.height());
		info("left: " + cropRect.left
			+ ", top: " + cropRect.top 
			+ ", width: " + cropRect.width() 
			+ ", height: " + cropRect.height()
			+ ", src.width: " + src.getWidth()
			+ ", src.height: " + src.getHeight());
		//return null;
		return Bitmap.createBitmap(src, 
				cropRect.left,
				cropRect.top,
				cropRect.width(), 
				cropRect.height(),
				matrix,
				true);
		
		/*return reduceBitmap(src, 
				dip2px(Constants.LargeImageWidth),
				dip2px(Constants.LargeImageHeight));*/
	}
	
	public void onCrop(View v) {
		Rect selectRect = imageCrop.getSelectionRect();
		Bitmap srcBmp = imageCrop.getBitmap();
		if (selectRect != null && srcBmp != null) {
			Bitmap crop = cropImage(srcBmp, selectRect);
			if (crop != null) {
				File file = PhotoUtils.saveAvatar(crop, me.getTelNo());
				if (file == null) {
					return;
				}
				Intent i = new Intent();
				i.setData(Uri.fromFile(file));
				i.putExtra("cropImagePath", file.getAbsolutePath());
				setResult(Activity.RESULT_OK, i);
				finish();
			}
		}
		
	}
}
